home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / getname.zip / GETNAME.C < prev    next >
Text File  |  1986-11-07  |  2KB  |  60 lines

  1. /**
  2.   getname - locates the name of the program as loaded by MS-DOS.
  3.     This is done by locating the end of the MS-DOS environment
  4.     table, and examining what follows. According to the DOS 
  5.     Technical Reference Manual, beginning with DOS 3.10:
  6.  
  7.     Load or Execute a Program (EXEC) - Function 4BH
  8.  
  9.     "Following the byte of zero [which follows the last ASCIIZ
  10.      string] in the environment, is a WORD that indicates the 
  11.      number of other strings following. Following this is a copy
  12.      of the DS:DX filename passed to the child process."
  13.  
  14.     Function call 4BH includes a pointer (DS:DX) to an ASCIIZ
  15.     string which specifies the module to be loaded. It may contain
  16.     a fully qualified drive/path/name/extension, or less. Since
  17.     COMMAND.COM uses Interrupt 21H, function 4BH to load new 
  18.     programs, this module should work on any system using DOS 3.10
  19.     or above. It most definitely does NOT work for DOS 3.00 or 
  20.     earlier.
  21.  
  22.     Public Domain by Peter Diehr to demonstrate how the module name
  23.     can be retrieved. If converted to subroutine form, this module
  24.     can be called by a Lattice C program, version 3.00 or later.
  25.     Written November 6, 1986 in Ann Arbor, Michigan.
  26. **/
  27. /**/
  28. #include <stdio.h>
  29. void main(argc,argv,envp)
  30. int argc;
  31. char *argv[];
  32. char *envp[];
  33. {
  34. extern char _DOS[2];        /* DOS version & level */
  35. extern char *environ[];    /* Environment pointer table address*/
  36. char *q;            /* used to manipulate strings */
  37. char **tmp;            /* used to manipulate pointer array*/
  38. int i;
  39. for(tmp=environ[0];*tmp;q=*tmp,tmp++) /* find end of environment */
  40.    ;
  41. for(;*q;q++)            /* locate the end of environment */
  42.    ;                /* q points at the last env.string*/
  43. q++;                /* push past 0 byte at end of string*/
  44. q++;                /* push past 0 string at end of table*/
  45. i=*(int *)q;            /* pointer to counter WORD */
  46. q++;q++;            /* push past WORD in two steps*/
  47. if(_DOS[0] >= 3 && _DOS[1] > 0 && i > 0)    /* must be DOS 3.10 +*/
  48.    {
  49.    printf("\nDOS Version %d.%d stored the name ",_DOS[0],_DOS[1]);
  50.    printf("\nIt was first among %d string(s) following the environment\n%s",i,q);
  51.    printf("\nLattice C passed a name of %s",argv[0]);
  52.    }
  53. else
  54.    {
  55.    printf("\nDOS Version %d.%d did not store the name\n",_DOS[0],_DOS[1]);
  56.    printf("String count was %d",i);
  57.    printf("\nLattice C passed a name of %s",argv[0]);
  58.    }
  59. }
  60.